<template>
  <div class="innerPageTable-box-OneTwo innerPageTable-out-flex">
     <!-- 搜索区域 -->
    <SelfAdaption>
      <Form
        :model="searchForm"
        ref="modelForm"
        slot="adaption-bar"
        label-position="top"
        class="ivu-form-no-margin-bottom"
        inline
      >
        <Form-item prop="name" :label="$t('module.launchApp.processTitle')">
          <Input
            type="text"
            :maxlength="30"
            v-model.trim="searchForm.name"
            placeholder
            @on-enter="search"
          ></Input>
        </Form-item>
        <Form-item prop="taskName" :label="$t('module.launchApp.taskName')">
          <Input
            type="text"
            :maxlength="30"
            v-model.trim="searchForm.taskName"
            placeholder
            @on-enter="search"
          ></Input>
        </Form-item>
        <Form-item prop="button" class="search-solt-btn">
           <RPButton optType="search" @click="search" />
           <RPButton optType="reset" @click="reset" />
        </Form-item>
      </Form>
    </SelfAdaption>
    <InnerPageTable ref="modelListTable" :config="tableConfig">
    </InnerPageTable>
  </div>
</template>
<script>
import ModelEdit from './ModelEdit.vue'
import { getAppHistoryList } from '@api/flowable/appHistory.js'
import { appListColumns } from './columns.js'
import IframeContainer from '@/components/IframeContainer.vue'
export default {
  name: 'menu-resource',
  props: {
    procTypeId: {
      type: String,
      default() {
        return ''
      }
    }
  },
  components: { IframeContainer },
  data() {
    return {
      isButtonDisabled: !this.procTypeId || this.procTypeId === '0',
      searchForm: {
        name: '',
        taskName: ''
      },
      tableConfig: {
        loadDataFn: ({ responseProcess, param }) => {
          getAppHistoryList(param).then(({ data }) => {
            data.currentPage = data.pageNum
            delete data.pageNum
            data.recordCount = data.total
            delete data.total

            data.records.map(item => {
              if (!item.endTime) {
                item.status = this.$t('module.launchApp.inProgress')
              } else {
                item.status = this.$t('module.launchApp.done')
              }
            })

            data.recordList = data.records
            delete data.records

            responseProcess(data)
          })
        },
        columnsFn: appListColumns.bind(this),
        autoFirst: true,
        loading: false
      }
    }
  },
  methods: {
    _launchProcess(processId) {
      let vm = this
      new this.$pageModal(
        IframeContainer,
        {
          attrs: {
            src: '/static/task/workflow/#/task/' + processId,
            width: 900,
            height: 700
          }
          // full: true,
          // isNav: false
        },
        // () => {}
        res => {
          res && vm.search()
        }
      )
    },
    search() {
      //刷新列表
      this.$refs['modelListTable'].load(this.searchForm)
    },
    reset() {
      this.$refs['modelForm'].resetFields()
      this.search()
    },
    designModel(modelId) {
      new this.$sidelipPage(
        IframeContainer,
        {
          attrs: {
            src: '/static/#/editor/' + modelId
          },
          full: true,
          isNav: false
        },
        () => {}
      )
    },
    addModel() {
      let modelInfo = {}
      modelInfo.procTypeId = this.procTypeId
      new this.$pageModal(
        ModelEdit,
        {
          props: {
            modelInfo
          }
        },
        data => {
          if (data) {
            this.search()
          }
        }
      )
    },
    importModel() {
      let modelInfo = { procTypeId: this.procTypeId, optType: 'import' }
      new this.$pageModal(
        ModelEdit,
        {
          props: {
            modelInfo
          }
        },
        data => {
          if (data) {
            this.search()
          }
        }
      )
    },
    editModel(modelInfo) {
      modelInfo.procTypeId = this.procTypeId
      new this.$pageModal(
        ModelEdit,
        {
          props: {
            modelInfo
          }
        },
        data => {
          if (data) {
            this.search()
          }
        }
      )
    },
    _deleteModel(data) {
      new $confirm(this.$t('common.warning.confirmToDelete'), this).then(() => {
        data.procTypeId = this.procTypeId
        deleteModel(data).then(res => {
          this.$Message.success({
            content: this.$t('common.deleteSuccess'),
            duration: 3
          })
          this.search()
        })
      })
    },
    _exportModel(modelId) {
      exportModel(modelId).then(res => {
        const { headers, data } = res
        let contentType = headers['content-type']
        const hrefValue = window.URL.createObjectURL(data)
        const fileInfo = res.headers['content-disposition'].split(';')[1]
        const a = document.createElement('a')
        a.setAttribute('href', hrefValue)
        a.download = decodeURIComponent(fileInfo && fileInfo.split('=')['1'])
        document.body.append(a)
        a.click()
        a.remove()
      })
    },
    _copyModel(modelInfo) {
      modelInfo.procTypeId = this.procTypeId
      modelInfo.optType = 'copy'
      new this.$pageModal(
        ModelEdit,
        {
          props: {
            modelInfo
          }
        },
        data => {
          if (data) {
            this.search()
          }
        }
      )
    }
  },
  watch: {
    procTypeId(newVal, oldVal) {
      if (oldVal !== newVal) {
        this.isButtonDisabled = !newVal || newVal === '0'
        this.searchForm.procTypeId = newVal
        this.search()
      }
    }
  }
}
</script>